//
//  XGStatusDetailViewController.swift
//  TodayNews
//
//  Created by monkey on 2020/3/12.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit
import MJRefresh

class XGStatusDetailViewController: UIViewController
{
    init(statusViewModel: XGStatusViewModel)
    {
        self.statusViewModel = statusViewModel
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 控制器生命周期方法
    
    override func loadView()
    {
        super.loadView()
        setUpUI()
    }
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        navigationItem.title = "动态详情"
        
        if statusViewModel.statusComments.count == 0 { // 没有评论数据进行加载
            loadData()
        } else {  // 已有评论数据
            statusViewModel.isHasMoreComments ? () : tableView.mj_footer?.endRefreshingWithNoMoreData()
        }
    }
    
    override func viewWillAppear(_ animated: Bool)
    {
        super.viewWillAppear(animated)
        navigationController?.setNavigationBarHidden(false, animated: animated)
    }
    // MARK: - 私有属性
    
    private lazy var tableView: UITableView = {
        let tableView = UITableView(frame: .zero, style: .plain)
        return tableView
    }()
    private let statusViewModel: XGStatusViewModel
}

// MARK: - 数据源和代理

extension XGStatusDetailViewController: UITableViewDataSource,UITableViewDelegate
{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
    {
        return statusViewModel.statusComments.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
    {
        let cell = tableView.xg_dequeueReusableCell(indexPath: indexPath) as XGStatusCommentTableViewCell
        cell.statusCommentViewModel = statusViewModel.statusComments[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat
    {
        return statusViewModel.statusComments[indexPath.row].rowHeight
    }
}

// MARK: - 私有扩展

private extension XGStatusDetailViewController
{
    @objc func loadData() -> Void
    {
        statusViewModel.fetchStatusComments { (isSuccess, hasMore) in
            hasMore ? self.tableView.mj_footer?.endRefreshing() : self.tableView.mj_footer?.endRefreshingWithNoMoreData()
            isSuccess ? self.tableView.reloadData() : ()
        }
    }
    
    func setUpUI() -> Void
    {
        view.backgroundColor = UIColor.white 
        view.addSubview(tableView)
        tableView.frame = CGRect(x: 0, y: 0, width: view.width, height: view.height - TodayNews_Bottom_Margin)
        
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.dataSource = self
        tableView.delegate = self
        let headerView = XGStatusDetailHeaderView(statusViewModel: statusViewModel)
        tableView.tableHeaderView = headerView
        tableView.tableFooterView = UIView()
        tableView.xg_registerCell(cellType: XGStatusCommentTableViewCell.self)
        tableView.mj_footer = MJRefreshBackNormalFooter(refreshingTarget: self, refreshingAction: #selector(loadData))
        tableView.contentInset = UIEdgeInsets(top: TodayNews_NavigationBar_Height, left: 0, bottom: 0, right: 0)
    }
}
